package com.ruoyi.utils;

import common.StOrbitPoint;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 临时  解析陨落数据使用
 */
public class TempFallUtil {
    public static List<StOrbitPoint>  analysis(String filePath) throws Exception {
        BufferedReader ifstream = new BufferedReader(new FileReader(filePath));
        int iNum = 0;
        ifstream.readLine();
        ifstream.readLine();
        ifstream.readLine();
        ifstream.readLine();
        int datalinesperepoch = 4;
        String line = "";
        List<StOrbitPoint> list = new ArrayList<>();
        while ((line = ifstream.readLine()) != null) {
            iNum++;
            if (iNum != 1) {
                if (iNum  == datalinesperepoch ) {
                    iNum = 0;
                }
                continue;
            }
            String[] stringVec = line.split("\\s");
            StOrbitPoint sptJ2000 = new StOrbitPoint();
            String timeStr = stringVec[0];
            //2021285164530
            int year = Integer.valueOf(timeStr.substring(0, 4));
            int doy = Integer.valueOf(timeStr.substring(4, 7));

            String str = timeStr.substring(7, 13);
            LocalTime localTime = LocalTime.parse(str,DateTimeFormatter.ofPattern("HHmmss"));
            LocalDate localDate = LocalDate.ofYearDay(year, doy);
            LocalDateTime of = LocalDateTime.of(localDate, localTime);
            String format = of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

            sptJ2000.setStrEpoch(format);
            sptJ2000.dx = Double.valueOf(stringVec[1])*1e3;//单位从km转换到m
            sptJ2000.dy = Double.valueOf(stringVec[2])*1e3;
            sptJ2000.dz = Double.valueOf(stringVec[3])*1e3;
            sptJ2000.dvx = Double.valueOf(stringVec[4])*1e3;
            sptJ2000.dvy = Double.valueOf(stringVec[5])*1e3;
            sptJ2000.dvz = Double.valueOf(stringVec[6])*1e3;
            list.add(sptJ2000);
        }
        return list;
    }

    public static boolean readStarlinkOrbitData(String filename, List<StOrbitPoint> satorbj2k) {
        String satelliteFilePath = filename;

        try {
            BufferedReader br = new BufferedReader(new FileReader(satelliteFilePath));
            Throwable var4 = null;

            try {
                int i = 0;
                int commentlines = 5;
                int datalinesperepoch = 4;
                int lastlines = commentlines + 468 * datalinesperepoch;

                String strLine;
                while((strLine = br.readLine()) != null) {
                    ++i;
                    if (i >= commentlines) {
                        if (i > lastlines) {
                            break;
                        }

                        if ((i - commentlines) % datalinesperepoch == 0) {
                            String[] strLineLst = strLine.split("\\s+");
                            int iSize = strLineLst.length;
                            int iYear = Integer.parseInt(strLineLst[0].substring(0, 4));
                            int idoy = Integer.parseInt(strLineLst[0].substring(4, 7));
                            int iHour = Integer.parseInt(strLineLst[0].substring(7, 9));
                            int iMinute = Integer.parseInt(strLineLst[0].substring(9, 11));
                            double dSecond = Double.parseDouble(strLineLst[0].substring(11, 17));
                            LocalDateTime curtime = LocalDateTime.now();
                            curtime = curtime.withDayOfYear(idoy + 1);
                            curtime = curtime.withYear(iYear);
                            curtime = curtime.withHour(iHour);
                            curtime = curtime.withMinute(iMinute);
                            curtime = curtime.withSecond((int)dSecond);
                            curtime = curtime.withNano((int)((dSecond - (double)((int)dSecond)) * 1.0E9));
                            int iMonth = curtime.getMonth().getValue();
                            int iDay = curtime.getDayOfMonth();
                            StOrbitPoint stOrbitPt = new StOrbitPoint();
                            String strYear = String.format("%04d", iYear);
                            String strMonth = String.format("%02d", iMonth);
                            String strDay = String.format("%02d", iDay);
                            String strHour = String.format("%02d", iHour);
                            String strMinute = String.format("%02d", iMinute);
                            String strSecond = String.format("%5.3f", dSecond);
                            stOrbitPt.strEpoch = strYear + "-" + strMonth + "-" + strDay + " " + strHour + ":" + strMinute + ":" + strSecond;
                            stOrbitPt.dx = Double.parseDouble(strLineLst[1]) * 1000.0;
                            stOrbitPt.dy = Double.parseDouble(strLineLst[2]) * 1000.0;
                            stOrbitPt.dz = Double.parseDouble(strLineLst[3]) * 1000.0;
                            stOrbitPt.dvx = Double.parseDouble(strLineLst[4]) * 1000.0;
                            stOrbitPt.dvy = Double.parseDouble(strLineLst[5]) * 1000.0;
                            stOrbitPt.dvz = Double.parseDouble(strLineLst[6]) * 1000.0;
                            satorbj2k.add(stOrbitPt);
                        }
                    }
                }
            } catch (Throwable var42) {
                var4 = var42;
                throw var42;
            } finally {
                if (br != null) {
                    if (var4 != null) {
                        try {
                            br.close();
                        } catch (Throwable var41) {
                            var4.addSuppressed(var41);
                        }
                    } else {
                        br.close();
                    }
                }

            }
        } catch (IOException var44) {
            var44.printStackTrace();
        }

        return true;
    }

}
